// 传统⽅式的实现


class Solution
{
public:
    int evalRPN(vector<string> &tokens)
    {
        stack<int> st;
        for (auto &str : tokens)
        {
            if (str == "+" || str == "-" || str == "*" || str == "/")
            {
                int right = st.top();
                st.pop();
                int left = st.top();
                st.pop();
                switch (str[0])
                {
                case '+':
                    st.push(left + right);
                    break;
                case '-':
                    st.push(left - right);
                    break;
                case '*':
                    st.push(left * right);
                    break;
                case '/':
                    st.push(left / right);
                    break;
                }
            }
            else
            {
                st.push(stoi(str));
            }
        }
        return st.top();
    }
};
// 使⽤map映射string和function的⽅式实现
// 这种⽅式的最⼤优势之⼀是⽅便扩展，假设还有其他运算，我们增加map中的映射即可

class Solution
{
public:
    int evalRPN(vector<string> &tokens)
    {
        stack<int> st;
        // function作为map的映射可调⽤对象的类型
        map<string, function<int(int, int)>> opFuncMap = {
            {"+", [](int x, int y)
             { return x + y; }},
            {"-", [](int x, int y)
             { return x - y; }},
            {"*", [](int x, int y)
             { return x * y; }},
            {"/", [](int x, int y)
             { return x / y; }}};
        for (auto &str : tokens)
        {
            if (opFuncMap.count(str)) // 操作符
            {
                int right = st.top();
                st.pop();
                int left = st.top();
                st.pop();
                int ret = opFuncMap[str](left, right);
                st.push(ret);
            }
            else
            {
                st.push(stoi(str));
            }
        }
            return st.top();
    }
};
